package com.kbs.pay.common.util;

import com.kbs.pay.common.util.support.IdWorker;
import org.apache.commons.lang3.StringUtils;

public class IdUtils {

	private static IdWorker idWorker = null;

	static {
		// 根据配置信息获取参数IdWorker(long workerId)的值
		String workIdStr = null;
		long workId = 1L;
		try {
			workIdStr = IpUtils.getLocalIp();
			if (workIdStr != null && workIdStr.length() != 0) {
				String[] ipSegments = workIdStr.split("\\.");
				workId = Double.valueOf(StringUtils.trim(ipSegments[3])).longValue();
			}
		} catch (Exception e) {
		}
		idWorker = new IdWorker(workId);
	}

	/**
	 * 返回long类型分布式ID
	 */
	public static long getId() {
		try {
			return idWorker.nextId();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * 返回自动生成的Id列表
	 */
	public static String getStrId() {
		try {
			return String.valueOf(idWorker.nextId());
		} catch (Exception e) {
			throw new RuntimeException(e);
		}

	}
}
